package com.cy.sys.service.impl;

import com.cy.common.annotation.RequiredLog;
import com.cy.common.exception.ServiceException;
import com.cy.common.pojo.PageObject;
import com.cy.sys.dao.SysLogDao;
import com.cy.sys.pojo.SysLog;
import com.cy.sys.service.SysLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public final class SysLogServiceImpl implements SysLogService {
    @Autowired
    private SysLogDao sysLogDao;

    @Override
    public void saveObject(SysLog entity) {
        String tName=Thread.currentThread().getName();
        System.out.println("SysLogServiceImpl.saveObject->thread.name->"+tName);
        try{Thread.sleep(5000);}catch (Exception e){}
        sysLogDao.insertObject(entity);
    }

    @RequiredLog("日志删除")
    @Override
    public int deleteObjects(Integer... ids){
        //1.参数校验
        if(ids==null||ids.length==0)
            throw new IllegalArgumentException("必须提供正确的id值");
        //2.基于id删除日志
        int rows=sysLogDao.deleteObjects(ids);
        //3.校验结果并返回
        if(rows==0)
            throw new ServiceException("记录可能已经不存在");
        return rows;
    }

    @RequiredLog("日志查询")
    @Override
    public PageObject<SysLog> findPageObjects(String username, Integer pageCurrent) {
        String tName=Thread.currentThread().getName();
        System.out.println("SysLogServiceImpl.findPageObjects->thread.name->"+tName);
        //long t1=System.currentTimeMillis();
        //1.验证参数合法性//参数校验
        //1.1验证pageCurrent的合法性，
        //不合法抛出IllegalArgumentException异常
        if(pageCurrent==null||pageCurrent<1) throw new IllegalArgumentException("当前页码不正确");
        //2.基于条件查询总记录数(查询总记录数并校验)
        //2.1) 执行查询
        int rowCount=sysLogDao.getRowCount(username);
        //2.2) 验证查询结果，假如结果为0不再执行如下操作
        if(rowCount==0) throw new ServiceException("系统没有查到对应记录");
        //3.基于条件查询当前页记录(pageSize定义为2)
        //3.1)定义pageSize
        int pageSize=5;
        //3.2)计算startIndex
        int startIndex=(pageCurrent-1)*pageSize;
        //3.3)执行当前数据的查询操作
        List<SysLog> records=sysLogDao.findPageObjects(username, startIndex, pageSize);
        //4.对分页信息以及当前页记录进行封装并返回(封装查询结果并返回)
        /*
        //4.1)构建PageObject对象
        PageObject<SysLog> po=new PageObject<>();
        //4.2)封装数据
        po.setPageCurrent(pageCurrent);
        po.setRowCount(rowCount);
        po.setRecords(records);
        po.setPageSize(pageSize);
        po.setPageCount((rowCount-1)/pageSize+1);
//      int pageCount=rowCount/pageSize;
//      if(rowCount%pageSize!=0)pageCount++;
//      po.setPageCount(pageCount);
        //5.返回封装结果。
        return po;
         */
        return new PageObject<>(pageCurrent,rowCount,records,pageSize);
        /*
        PageObject<SysLog> pageObject= new PageObject<>(rowCount,records,pageCurrent,pageSize);
        // long t2=System.currentTimeMillis();
        // log.info("time {}",t2-t1);
        return pageObject;
         */
    }
}
